#include "utils.h"
#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

std::vector<std::string> str2words(std::string str)
{

    std::istringstream iss(str);
    std::vector<std::string> v;
    do
    {
        std::string s;
        iss >> s;
        v.push_back(s);
    }
    while (iss);

    if(v.size() > 0 && v.back().size() == 0)
            v.pop_back();

    return v;
}

int getFileSize(string filename)
{

    struct stat filestatus;
    if(stat( filename.c_str(), &filestatus ) != 0)
        return -1;
    return filestatus.st_size;

}

std::string getFilenameFromPath(std::string path)
{

    const size_t last_slash_idx = path.find_last_of("\\/");
    if (std::string::npos != last_slash_idx)
        path.erase(0, last_slash_idx + 1);
    return path;
}

void create_dir(string path)
{
    ostringstream oss;
    oss << "mkdir -p ";
    oss << path;
    system(oss.str().c_str());
}

std::string getFilenameVersion(std::string directory, std::string filename, int version)
{
    ostringstream oss;
    oss << directory << '/';
    oss << 'v' << version << '_' << filename;
    return oss.str();
}

std::string getRedColor()
{
    char color[80] = {0};
    sprintf(color, "%c[%d;%d;%dm", 0x1B, 1, 1 + 30, 0 + 40);
    return string(color);

}
std::string getWhiteColor()
{
    char color[80] = {0};
    sprintf(color, "%c[%d;%d;%dm", 0x1B, 0, 2 + 30, 0 + 40);
    return string(color);
}

